package com.hugoo.sch;

import com.hugoo.core.Conf;
import com.hugoo.core.util.FTP;
import com.hugoo.core.util.GZipUtils;
import com.hugoo.core.util.TL;
import com.hugoo.imp.buzz.ImpDataBuzz;
import it.sauronsoftware.cron4j.Task;
import it.sauronsoftware.cron4j.TaskExecutionContext;
import java.io.File;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Lovelock.Chan
 */
public class FtpDownload extends Task {

    private static final Log log = LogFactory.getLog(FtpDownload.class);
    private String sid;

    public FtpDownload(String sid) {
        this.sid = sid;
    }

    @Override
    public void execute(TaskExecutionContext context) throws RuntimeException {
        String svr = Conf.getBaseProp(sid, Conf.FTP._ftp_server);
        if (TL.isEmpty(svr)) {
            log.error("调度FTP下载配置的FTP服务器项【ftp.server】无效，请重新配置。");
        } else {
            FTP ftp = new FTP(svr);
            String ws = Conf.getBaseProp(sid, Conf.FTP._ftp_workspace);
            String fp = Conf.getBaseProp(sid, Conf.FTP._ftp_filename_pattern);
            if (TL.isEmpty(ws, fp)) {
                //参数不足
                log.error("调度FTP下载配置的FTP服务器存储空间【ftp.workspace】或存储文件命名规则【ftp.filename.pattern】无效，请重新配置。");
            } else {
                String filename = (ws.endsWith("/") ? ws : (ws + "/")) + (fp.replace("{yyyyMMdd}", TL.format(new Date(), "yyyyMMdd")));
                String scod = TL.socd();
                String ext = filename.substring(filename.lastIndexOf("."));
                boolean nd = false;
                if (".gz".equalsIgnoreCase(ext)) {
                    nd = true;
                }
                File tmp = initFile(scod, ext);
                //下载文件数据
                if (ftp.download(filename, tmp)) {
                    if (nd) {
                        try {
                            tmp = GZipUtils.decompress(tmp);
                        } catch (Exception ex) {
                            log.warn("解压文件出错！", ex);
                        }
                    }
                    //存储基本媒体数据
                    if (nd) {
                        filename = filename.substring(0, filename.lastIndexOf("."));
                        ext = ".txt";
                    }
                    ImpDataBuzz.ref().saveAndValidationFileMeta(sid, scod, "", "SYS", filename.substring(filename.lastIndexOf("/") + 1), tmp.getAbsolutePath(),
                            tmp.length(), ext, null);
                } else {
                    log.error("下载存储文件失败！");
                }
            }
        }
    }

    private File initFile(String uuid, String ext) {
        String basepath = Conf.getBaseProp(Conf._imp_file_space);
        String mpath = TL.format(new Date(), "/yyyy/MM/");
        String bash = basepath + mpath;
        File baseFile = new File(bash);
        if (!baseFile.exists()) {
            baseFile.mkdirs();
        }
        return new File(bash + uuid + ext);
    }
}
